/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tetris;

import GA.IEvolvingAI;
import java.awt.Color;
import NN.*;

/**
 *
 * @author S094182
 */
public class NNAi extends TetrisAI implements IEvolvingAI {
    
    private Network net = new Network(200, 1, new int[] { 40 });
    private double[] prevInputs;
    public NNAi() {
       
    }
    
    @Override
    public void setHeuristic(Object rule) {
        net.readBinaryGenome((byte[])rule);
        prevInputs = null;
    }

    @Override
    double rateBoard(Color[][] board) {
        double[] inputs = new double[200];
        int i = 0;
        for (Color[] colors : board) {
            for (Color color : colors) {
                if (color != null)
                    inputs[i++] = 1;
                else inputs[i++] = 0;                
            }
        }
        double[] res = net.processInput(inputs);
        if(prevInputs != null) {
            net.processInput(prevInputs, res, 0.2);

        }
        prevInputs = inputs; 
            
        return res[0];
    }

    @Override
    public int getNGenes() {
        return net.getNoWeights()*4;
    }

    @Override
    public void feedback(Object obj) {
        net.processInput(prevInputs, (double[])obj, 0.4);
    }

}
